﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Web.Script.Services;
using SapB1WebPOS.BLL;
using System.Data;
using Newtonsoft.Json;
using SapB1WebPOS.Utils;

namespace SapB1WebPOS
{
    public partial class PurchaseRequestList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        /// <summary>
        /// Carcard !="" : Copy from PO
        /// </summary>
        /// <param name="keySearch"></param>
        /// <param name="cardCode"></param>
        /// <returns></returns>
        [WebMethod]
        [ScriptMethod(UseHttpGet = true, ResponseFormat = ResponseFormat.Json)]
        public static string GetList(string keySearch)
        {
            try
            {
                string companyDB = string.Empty;

                if (HttpContext.Current.Session["CompanyDB"] != null)
                {
                    companyDB = HttpContext.Current.Session["CompanyDB"].ToString();
                }
                if (companyDB.Length > 0)
                {
                    PurchaseRequestBLL obj = new PurchaseRequestBLL();

                    if (keySearch.Length == 0)
                    {
                        long count = 1;
                        long maximunDisplay;
                        long.TryParse(System.Configuration.ConfigurationManager.AppSettings["MaximunDisplayItems"].ToString(), out maximunDisplay);
                        if (count <= maximunDisplay)
                        {
                            DataSet dsData = null;

                            dsData = obj.GetDocuments(companyDB);

                            if (dsData != null)
                            {
                                return JsonConvert.SerializeObject(dsData.Tables[0]);
                            }
                        }
                        else
                        {
                            DataTable tbFormat = new DataTable();
                            tbFormat.Columns.Add("DocEntry");
                            tbFormat.Columns.Add("DocNum");
                            tbFormat.Columns.Add("DocStatus");
                            tbFormat.Columns.Add("CANCELED");
                            tbFormat.Columns.Add("ReqName");
                            tbFormat.Columns.Add("DocTotal");
                            tbFormat.Columns.Add("Comments");
                            tbFormat.Columns.Add("DocDate");
                            tbFormat.Columns.Add("ReqDate");
                            tbFormat.Columns.Add("TaxDate");
                            return JsonConvert.SerializeObject(tbFormat);
                        }
                    }
                    else
                    {
                        DataSet dsData = null;
                        dsData = obj.GetDocumentForSearch(companyDB, keySearch);
                        if (dsData != null)
                        {
                            return JsonConvert.SerializeObject(dsData.Tables[0]);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Log.WriteException(System.Reflection.MethodBase.GetCurrentMethod().DeclaringType, "SystemError", ex);
            }
            return string.Empty;
        }
    }
}